package base.day14.controller;

import base.day14.domain.Teacher;
import base.day14.service.TeacherService;

import java.awt.*;
import java.util.Scanner;

public class TeacherController {

    TeacherService ts=new TeacherService();
    Teacher teacher=new Teacher();
    private Scanner sc=new Scanner(System.in);
    public void start() {
        wc:while (true) {
            System.out.println("------------<老师>管理系统----------");
            System.out.println("1 添加老师 \t 2 删除老师 \t 3 修改老师 \t 4 查看老师 \t 5 退出");
            System.out.println("请输入你的选择：");
            String st = sc.next();
            switch (st) {
                case "1":
                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    System.out.println("修改老师");
                    upDateTeacher();
                    break;
                case "4":
                    System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("已退出老师管理系统");
                    break wc;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }

    private void upDateTeacher() {
        String upid;
        while(true){
            System.out.println("请输入要修改的id");
            upid=sc.next();
            boolean exeists=ts.isExists(upid);
            if (!exeists){
                System.out.println("未找到id，请重新输入");
            }else{
                break;
            }
        }
        System.out.println("请输入姓名：");
        String name=sc.next();
        System.out.println("请输入年龄：");
        int age=sc.nextInt();
        System.out.println("请输入生日：");
        String birthday=sc.next();
        teacher.setId(upid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        ts.upDateTeacher(upid,teacher);
        System.out.println("修改成功");
    }

    private void deleteTeacherById() {
        String deid;
        while (true){
            System.out.println("请输入要删除的id");
            deid=sc.next();
            boolean exeists=ts.isExists(deid);
            if (!exeists){
                System.out.println("未查询到id");
            }else {
                break;
            }
        }
        ts.deleteTeacherById(deid);
        System.out.println("删除成功");
    }

    private void findAllTeacher() {
        Teacher[] teas=ts.findAllStudent();
        if (teas==null){
            System.out.println("未添加信息请稍后再试");
            return;
        }else {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (int i = 0; i < teas.length; i++) {
                Teacher tea=teas[i];
                if (tea!=null){
                    System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t"+tea.getBirthday());
                }
            }
        }
    }

    private void addTeacher() {
        String id;
        while (true){
            System.out.println("请输入id");
            id=sc.next();
            boolean flag =ts.isExists(id);
            if (flag){
                System.out.println("id已存在，请重新输入");
            }else{
                System.out.println("请输入姓名：");
                String name=sc.next();
                System.out.println("请输入年龄：");
                int age=sc.nextInt();
                System.out.println("请输入生日：");
                String birthday=sc.next();
                teacher.setId(id);
                teacher.setName(name);
                teacher.setAge(age);
                teacher.setBirthday(birthday);
                boolean result=ts.addTeacher(teacher);
                if (result){
                    System.out.println("添加成功");
                    break;
                }else{
                    System.out.println("添加失败");
                }

            }
        }
    }
}
